package com.jie.server.modules.system.service.impl;

import cn.hutool.core.collection.CollectionUtil;
import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.jie.server.common.domain.TreeSelect;
import com.jie.server.modules.system.entity.AreaEntity;
import com.jie.server.modules.system.entity.Menu;
import com.jie.server.modules.system.mapper.AreaMapper;
import com.jie.server.modules.system.service.IAreaService;
import com.jie.server.modules.system.vo.AreaExcelVo;
import com.jie.server.modules.system.vo.AreaResultVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.Iterator;
import java.util.List;
import java.util.stream.Collectors;

/**
 * @description: 行政区划服务实现类
 * @author: xiaojie
 * @since: 2024/12/18 0018
 */
@Service
public class AreaServiceImpl extends ServiceImpl<AreaMapper, AreaEntity> implements IAreaService  {

    @Autowired
    private AreaMapper areaMapper;

    /**
     * 查询省市区表
     * @param areaId 省市区表主键
     * @return 省市区表
     */
    @Override
    public AreaEntity selectAreaByAreaId(Long areaId) {
        return areaMapper.selectAreaByAreaId(areaId);
    }

    /**
     * 查询省市区表列表
     * @param areaEntity 省市区表
     * @return 省市区表
     */
    @Override
    public List<AreaEntity> selectAreaList(AreaEntity areaEntity) {
        return areaMapper.selectAreaList(areaEntity);
    }

    /**
     * 新增省市区表
     * @param areaEntity 省市区表
     * @return 结果
     */
    @Override
    public boolean create(AreaEntity areaEntity) {
        return save(areaEntity);
    }

    /**
     * 修改省市区表
     * @param areaEntity 省市区表
     * @return 结果
     */
    @Override
    public boolean update(AreaEntity areaEntity) {
        return updateById(areaEntity);
    }

    /**
     * 批量删除省市区表
     * @param areaIds 需要删除的省市区表主键
     * @return 结果
     */
    @Override
    public int deleteAreaByAreaIds(Long[] areaIds) {
        return areaMapper.deleteBatchIds(Arrays.asList(areaIds));
    }

    /**
     * 删除省市区表信息
     * @param areaId 省市区表主键
     * @return 结果
     */
    @Override
    public int deleteAreaByAreaId(Long areaId) {
        return areaMapper.deleteById(areaId);
    }

    /**
     * 获取省份列表
     * @param type 区域类型
     * @param parentId 上级id
     * @return 结果
     */
    @Override
    public List<AreaResultVo> getAreaListByTypeAndParentId(Integer type, Long parentId) {
        List<AreaResultVo> volist = new ArrayList<>();
        if (parentId == null) {
            volist = baseMapper.getAreaList(type,null,null);
        } else {
            volist = baseMapper.getAreaList(type,parentId,null);
        }
        return volist;
    }

    /**
     * 获取市、区县列表
     * @param type 区域类型
     * @param name 名称
     * @param parentName 上级名称
     * @return 结果
     */
    @Override
    public List<AreaResultVo> getAreaListByTypeAndParentName(Integer type, String name,String parentName) {
        Long parentId = null;
        //市有重复的名称，查询区/县 要带上parentId
        if(StrUtil.isNotBlank(parentName)){
            List<AreaResultVo> volist = baseMapper.getAreaList(0,null,parentName);
            if (CollectionUtil.isNotEmpty(volist)) {
                parentId = volist.get(0).getAreaId();
            }
        }
        List<AreaResultVo> volist = baseMapper.getAreaList(type,parentId,name);
        if (CollectionUtil.isNotEmpty(volist)) {
            List<AreaResultVo> list = getAreaListByTypeAndParentId(type+1, volist.get(0).getAreaId());
            return list;
        }
        return null;
    }

    /**
     * 构建前端所需要下拉树结构
     * @param areas 行政区划列表
     * @return 下拉树结构列表
     */
    @Override
    public List<TreeSelect> buildAreaTreeSelect(List<AreaEntity> areas) {
        List<AreaEntity> returnList = new ArrayList<>();
        List<Long> tempList = new ArrayList<>();
        for (AreaEntity areaEntity : areas) {
            tempList.add(areaEntity.getAreaId());
        }
        for (Iterator<AreaEntity> iterator = areas.iterator(); iterator.hasNext(); ) {
            AreaEntity area = iterator.next();
            // 如果是顶级节点，遍历该父节点所有的子节点
            if (!tempList.contains(area.getParentId())) {
                recursionList(areas,area);
                returnList.add(area);
            }
        }
        if (CollectionUtil.isEmpty(returnList)) {
            returnList = areas;
        }
        return returnList.stream().map(TreeSelect::new).collect(Collectors.toList());
    }

    @Override
    public List<AreaExcelVo> listExprot() {
        List<AreaEntity> list = areaMapper.selectList(null);

        return list.stream()
                .map(e ->
                        AreaExcelVo.builder()
                                .name(e.getName())
                                .build()
                )
                .collect(Collectors.toList());
    }

    /**
     * 递归列表
     * @param areas
     * @param t
     */
    private void recursionList(List<AreaEntity> areas, AreaEntity t) {
        // 得到子节点
        List<AreaEntity> childList = getChildList(areas,t);
        t.setChildren(childList);
        for (AreaEntity tChild : childList) {
            if (hasChild(areas,tChild)) {
                recursionList(areas, tChild);
            }
        }
    }

    /**
     * 得到子节点列表
     */
    private List<AreaEntity> getChildList(List<AreaEntity> list, AreaEntity t) {
        List<AreaEntity> tList = new ArrayList<>();
        Iterator<AreaEntity> it = list.iterator();
        while (it.hasNext()) {
            AreaEntity n = it.next();
            if (n.getParentId().longValue() == t.getAreaId().longValue()) {
                tList.add(n);
            }
        }
        return tList;
    }
    /**
     * 判断是否有子节点
     */
    private boolean hasChild(List<AreaEntity> list, AreaEntity t) {
        return getChildList(list, t).size() > 0;
    }

}
